using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DialectSoftware.Web.UI.CustomUserControls;
using DialectSoftware.Web.JsAPI;
using System.ComponentModel;

namespace Demo.UserControls
{
    public partial class GridControl : DataBoundUserControl, IMultipleModeEventHandler
    {
        public event EventHandler<GridEventArgs> OnFilterGrid;


        public override object DataSource
        {
            set 
            {
                base.DataSource = value;
                this.Grid.DataSource = value;
                
            }
        }

        public override void DataBind()
        {
            base.DataBind();
        }

        protected override void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        [ClientSideVariable("mode")]
        [Category("Behavior")]
        public ControlMode Mode
        {
            get
            {
                return ViewState["mode"] == null ? ControlMode.Server : (ControlMode)ViewState["mode"];
            }
            set
            {
                ViewState["mode"] = value;
            }

        }

        [JsFunction()]
        public JsOperation Filter(var value)
        {
            String cbReference = String.Empty;
            String callbackScript = String.Empty;
            switch (Mode)
            {
                case ControlMode.Server:
                    cbReference = Page.ClientScript.GetPostBackEventReference(this, "$value$", false).Replace("'$value$'", "value");
                    callbackScript = "document.observe('dom:loaded',function(){" + this.OnFilter(value) + "});";
                    callbackScript = "if(this.loaded == false){this.loaded = true;" + callbackScript + "return;} " + cbReference + "";
                    break;
                case ControlMode.Ajax:
                    cbReference = Page.ClientScript.GetCallbackEventReference(this, "value", this.GetJsType().OnFilter, this.Grid.ClientID);
                    callbackScript = cbReference;
                    break;
                case ControlMode.Client:
                    callbackScript = "alert('Client Mode is not supported for this control');";
                    break;
            }

            function f = new function();
            f.AddOperation(callbackScript);
            return new JsOperation(new JsObject(this.ID, this.GetType().Name), f, value);

        }

        [JsFunction()]
        public JsOperation OnFilter(var value)
        {
            function f = new function();
            switch (Mode)
            {
                case ControlMode.Ajax:
                    f.AddOperation("document.getElementById('" + this.ClientID + "').innerHTML = value");
                    break;
                default:
                    f.AddOperation("");
                    break;
            }
            return new JsOperation(this, f, value);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            if (OnFilterGrid != null)
            {
                OnFilterGrid(this, new GridEventArgs() { Amount = eventArgument });
            }
        }

        public string GetCallbackResult()
        {
            System.IO.StringWriter s = new System.IO.StringWriter();
            HtmlTextWriter writer = new HtmlTextWriter(s);
            Render(writer);
            return s.ToString();
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            if (OnFilterGrid != null)
            {
                OnFilterGrid(this, new GridEventArgs() { Amount = eventArgument });
            }
        }
    }

    public class GridEventArgs : EventArgs
    {
        public string Amount { get; set; }
    }
}